using UnityEngine;
using UnityEngine.UI;
using QFramework;
using DG.Tweening;
using System.Collections.Generic;
using System;

namespace HareGame
{
    public class UITaskData : UIPanelData
    {
    }
    public partial class UITask : UIBase
    {
        [Header("阶段宝箱的钻石数量")]
        [SerializeField]
        private double DiamondNum = 10d;

        [HideInInspector]
        public List<TaskData> taskDatas = new List<TaskData>();

        [HideInInspector]
        public int completeCount = 0;

        [HideInInspector]
        public List<TaskItemUI> items = new List<TaskItemUI>();

        protected override void OnInit(IUIData uiData = null)
        {
            base.OnInit();
            mData = uiData as UITaskData ?? new UITaskData();
        }

        protected override void OnOpen(IUIData uiData = null)
        {
            base.OnOpen();

            GameEntry.Util.DoMoveUp(Bg);
            taskDatas = GameEntry.Task.GetStateTaskDatas();
            RefreshUI();

            btnClose.onClick.AddListener(OnCloseClick);

            btnStateReward.onClick.RemoveAllListeners();
            btnStateReward.onClick.AddListener(OnStateBoxClick);
            btnBox.onClick.AddListener(ShowRewardTips);
        }

        protected override void OnClose()
        {
            base.OnClose();

            foreach (var v in items)
            {
                v.RecycleSelf();
            }

            btnClose.onClick.RemoveListener(OnCloseClick);
            btnStateReward.onClick.RemoveListener(OnStateBoxClick);
            btnBox.onClick.RemoveListener(ShowRewardTips);
        }

        void RefreshUI()
        {
            foreach (var v in items)
            {
                v.RecycleSelf();
            }
            completeCount = 0;
            foreach (var v in taskDatas)
            {
                if (v.IsCompleted)
                {
                    completeCount++;
                }
                else
                {
                    TaskItemUI item = GameEntry.Pool.CreateObj<TaskItemUI>(QAssetBundle.Taskitemui_prefab.TaskItemUI, Content);
                    item.Init(v, OnTaskComplete, OnTaskGoClick);
                    items.Add(item);
                }
            }
            if (completeCount == taskDatas.Count)
            {
                Ready.gameObject.SetActive(false);
                Complete.gameObject.SetActive(true);
            }
            else
            {
                Ready.gameObject.SetActive(true);
                Complete.gameObject.SetActive(false);
                txtProgress.text = completeCount + "/" + taskDatas.Count;
                Progress.fillAmount = completeCount * 1.0f / taskDatas.Count;
            }
        }

        void ShowRewardTips()
        {
            RewardTips.transform.DOKill();
            RewardTips.transform.localScale = Vector3.zero;
            RewardTips.transform.DOScale(1, 0.25f).SetEase(Ease.OutBack).OnComplete(() =>
            {
                RewardTips.transform.DOScale(0, 0.25f).SetEase(Ease.InBack).SetDelay(2);
            });
        }

        void OnStateBoxClick()
        {
            GameEntry.Task.CompleteStateTask();
            GameEntry.Event.Send(EventID.AddReward, 1, DiamondNum);
            if (GameEntry.Task.IsCompleteAllTask)
            {
                OnCloseClick();
                GameEntry.Event.Send(EventID.CompleteAllTask);
            }
            else
            {
                taskDatas = GameEntry.Task.GetStateTaskDatas();
                RefreshUI();
            }
        }

        void OnTaskComplete()
        {
            RefreshUI();
        }

        void OnTaskGoClick(TaskData data)
        {
            Close(() =>
            {
                GameEntry.Task.OnTaskGoAction(data);
            });
        }

        public void OnCloseClick()
        {
            Close();
        }

        void Close(Action callback = null)
        {
            if (!m_HandleAble)
                return;

            m_HandleAble = false;
            GameEntry.Util.DoMoveUpBack(Bg).OnComplete(() =>
             {
                 GameEntry.UI.CloseUI(this);
                 callback?.Invoke();
             });
        }
    }
}
